package com.massfords.jaxb;

import java.util.Set;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JVar;
import com.sun.tools.xjc.outline.ClassOutline;

/**
 * Adds the accept method to the bean.
 * 
 * @author markford
 */
public class AddAcceptMethod {

    public void run(Set<ClassOutline> aSorted, JDefinedClass aVisitor) {
        for (ClassOutline classOutline : aSorted) {
            
            // add the accept method to the bean
            JDefinedClass beanImpl = classOutline.implClass;
            JMethod acceptMethod = beanImpl.method(JMod.PUBLIC, void.class, "accept");
            JVar vizParam = acceptMethod.param(aVisitor, "aVisitor");
            JBlock block = acceptMethod.body();
            JInvocation vizInvocation = block.invoke(vizParam, "visit");
            vizInvocation.arg(JExpr._this());
        }
    }
}
